<?php

namespace app\api\controller;

use app\common\controller\Api;
use fast\Http;
use think\Db;
use think\Log;

/**
 * 首页接口
 */
class Car extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    /**
     * header头
     */
    public function httpHeader()
    {
        //请求头参数
        $user            = 'test_car';
        $time            = time();

        $key              = "d20486e1e812bd28a6fc6b567a774df7";
        $sign1          = hash('sha256', "$user+$time");
        $sign2          = hash('sha256', "$sign1+$key");
        $header         =       [
                                    CURLOPT_HTTPHEADER  => [
                                                "user:$user",
                                                "unixtime:$time",
                                                "sign:$sign2"
                                    ]
                            ];
        return $header;
    }

     /**
     * 出险查询
     *
     * @ApiMethod (POST)
     * @param string  $vin 车辆识别码
     * @param string  $img 图片 URL 地址
     * @param string  $img_type 图片类型 vehicle 行驶证 ， vrc 汽车登记证书
     */
    public function httpSelect()
    {
        //接口参数
        $vin              = 'LJXCU2BB8KTP33931';
        $img             = domain() . "/uploads/car/1.jpg";
        $img_type    = 'vehicle';

        $url               = "https://openapi.chexinbaogao.com/v1/order/add";


        $data            =       [
                                        'vin' => $vin,
                                        'img' => $img,
                                        'img_type'   => $img_type,
                                ];

        $data= json_encode($data);

        $header = $this->httpHeader();
        mylog(['request' => ['json' => $data, 'header' => $header]], 'che');
        $result = Http::post($url, $data, $header);
        $result = json_decode($result);
        mylog(['response' => $result], 'che');
        $this->success('OK', $result);
    }

     /**
     * 更新报告
     *
     * @ApiMethod (POST)
     * @param string  $vin 车辆识别码
     * @param string  $id ''
     * @param string
     */
    public function updateReport()
    {
        //接口参数
        $vin              = 'LJXCU2BB8KTP33931';

        $url               = "https://openapi.chexinbaogao.com/v1/order/update";

        $id = input('id');
        // dump($id);
        $data            =       [
                                        'vin' => $vin,
                                        'id' => $id,
                                ];

        $data= json_encode($data);

        $header = $this->httpHeader();
        mylog(['request' => ['json' => $data, 'header' => $header]], 'che');
        $result = Http::post($url, $data, $header);
        $result = json_decode($result);
        mylog(['response' => $result], 'che');
        $this->success('OK', $result);
    }

    /**
     * 用户余额
     *
     * @ApiMethod (GET)
     */
    public function userMoney()
    {
        $url               = "https://openapi.chexinbaogao.com/v1/user/balance";


        $data            =       [];

        $data= json_encode($data);

        $header = $this->httpHeader();
        mylog(['request' => ['json' => $data, 'header' => $header]], 'che');
        $result = Http::get($url, $data, $header);
        $result = json_decode($result);
        mylog(['response' => $result], 'che');
        $this->success('OK', $result);
    }

    /**
     * 获取报告
     *
     * @ApiMethod (GET)
     */
    public function getReport()
    {
        $id = input('id');
        $url               = "https://openapi.chexinbaogao.com/v1/report/$id";


        $data            =       [];

        $data= json_encode($data);

        $header = $this->httpHeader();
        mylog(['request' => ['json' => $data, 'header' => $header]], 'che');
        $result = Http::get($url, $data, $header);
        $result = json_decode($result);
        mylog(['response' => $result], 'che');
        $this->success('OK', $result);
    }

    public function getData()
    {
        $url ='https://www.chexinbaogao.com/report?data=4e8e5f5a2592a651ec2a4d57b55c27b6ec7eba5fc4418a0d8e10c5a16ee6baa9e0b6bb0ebfffaa31cf1d33f013804f8bddebebfac0a003b41088b7fd1ccca2c5';
        $aa = file_get_contents($url);
        preg_match_all('/报告发布时间：(.*?)<\/div>/',$aa,$matches_times);

        preg_match_all('/报告发布时间：(.*?)<\/div>/',$aa,$matches_times);

        $pattern = $matches_times[1];
        $replacement = date('Y-m-d H:i:s');

        $aa = str_replace($pattern, $replacement, $aa);
        // dump($matches);
        $aa = str_replace('/img2022/qrcode.png', './img2022/qrcode.png', $aa);


        preg_match_all('/src="(.*?)"/',$aa,$matches_imgs_two);

        $matches_img_t = $matches_imgs_two[1];

        //确定图片有什么目录
        for ($i=0; $i < count($matches_img_t); $i++) {
                    dump($matches_img_t[$i]);

            if(strstr($matches_img_t[$i], 'png') || strstr($matches_img_t[$i], 'jpg') || strstr($matches_img_t[$i], 'jpeg'))
            {
                if(strstr($matches_img_t[$i], './') && !strstr($matches_img_t[$i], '../') )
                {
                    $str1 = ltrim($matches_img_t[$i], './');
                    $str2 = explode('/', $str1);
                    if($str2[0] == '')
                    {
                        continue;
                    }
                    $arr[] = $str2[0];

                }else if(!strstr($matches_img_t[$i], '../') ){
                    $str2 = explode('/', $matches_img_t[$i]);
                    if(count($str2) == 2)
                    {
                        if($str2[0] == '')
                        {
                            continue;
                        }
                        $arr[] = $str2[0];
                    }else{

                        $arr[] = $str2[0];
                    }

                }

                $last_img_path[] = $matches_img_t[$i];
                continue;
            }
        }

        //图片所有目录
        $image_path = array_values(array_unique($arr));

        //去创建该报告所有目录
        for ($i=0; $i < count($image_path); $i++) {

            if(!file_exists($_SERVER['DOCUMENT_ROOT'] . "/commons/html/1/" . $image_path[$i]))
            {
                mkdir($_SERVER['DOCUMENT_ROOT'] . "/commons/html/1/" . $image_path[$i], 0777, true);
            }
        }
        //IMAGE
        for ($i=0; $i < count($last_img_path); $i++) {


                if(strstr($last_img_path[$i], 'mx.png') || strstr($last_img_path[$i], 'bx.png') || strstr($last_img_path[$i], 'kx.png'))
                {
                    continue;
                }
                if(strstr($last_img_path[$i], './') && !strstr($last_img_path[$i], '../') )
                {
                    $str1 = ltrim($last_img_path[$i], '.');

                    $root = 'https://www.chexinbaogao.com' . $str1;

                    $body = file_get_contents($root);


                    file_put_contents($_SERVER['DOCUMENT_ROOT'] . "/commons/html/1" . $last_img_path[$i], $body);
                }else if(!strstr($last_img_path[$i], '../') ){
                    $root = 'https://www.chexinbaogao.com/' . $last_img_path[$i];

                    $body = file_get_contents($root);


                    file_put_contents($_SERVER['DOCUMENT_ROOT'] . "/commons/html/1/" . $last_img_path[$i], $body);
                }
        }

        //单独处理公共顶部
        $root = 'https://www.chexinbaogao.com/images2/bjj.png';
        $body = file_get_contents($root);
        file_put_contents($_SERVER['DOCUMENT_ROOT'] . "/commons/html/1/images2/bjj.png", $body);
        //单独处理公共LOGO
        $root = 'https://www.chexinbaogao.com/images2/bjj.png';
        $body = file_get_contents($root);
        file_put_contents($_SERVER['DOCUMENT_ROOT'] . "/commons/html/1/images2/bjj.png", $body);
        //单独处理公共汽车背景图
        $root = 'https://www.chexinbaogao.com/img2023/car_bg.png';
        $body = file_get_contents($root);
        file_put_contents($_SERVER['DOCUMENT_ROOT'] . "/commons/html/1/img2023/car_bg.png", $body);





        // die;

        preg_match_all('/href="(.*?)"/',$aa,$matches_t);
        preg_match_all('/<script src="(.*?)">(.*?)<\/script>/',$aa,$matches);

        $matches_t_str = $matches_t[1];

        $matches_str = $matches[1];


        //CSS
        for ($i=0; $i < count($matches_t_str); $i++) {
            if(strstr($matches_t_str[$i], 'font2') || strstr($matches_t_str[$i], 'html'))
            {
                continue;
            }

            if($matches_t_str[$i] == '' )
            {
                continue;
            }

            $str1 = ltrim($matches_t_str[$i], '.');

            $root = 'https://www.chexinbaogao.com' . $str1;
            $body = file_get_contents($root);

            if(strstr($matches_t_str[$i], 'css.css'))
            {
                preg_match_all('/url((.*))/',$body,$matches_css_image);

            }
            $new_root = explode('?', $str1);
            if(!file_exists($_SERVER['DOCUMENT_ROOT'] . "/commons/html/1/bg/css2"))
            {
                mkdir($_SERVER['DOCUMENT_ROOT'] . "/commons/html/1/bg/css2", 0777, true);
            }

            file_put_contents($_SERVER['DOCUMENT_ROOT'] . "/commons/html/1" . $new_root[0], $body);



        }
        $new_arrs = $matches_css_image[2];
        for ($i=0; $i < count($new_arrs) ; $i++) {
            dump($new_arrs[$i]);
            if(strstr($new_arrs[$i], "('"))
            {
                $arr = explode("('..", $new_arrs[$i]);
                $arr = explode("')", $arr[1]);

            }else{
                $arr = explode("(..", $new_arrs[$i]);
                $arr = explode(")", $arr[1]);

            }


            $root = 'https://www.chexinbaogao.com/bg' . $arr[0];

            $body = file_get_contents($root);


            file_put_contents($_SERVER['DOCUMENT_ROOT'] . "/commons/html/1/bg" . $arr[0], $body);
        }
        $image_css_background = '';
        //JS
        for ($i=0; $i < count($matches_str); $i++) {



            if(strstr($matches_str[$i], 'ggywadd') || strstr($matches_str[$i], 'html'))
            {
                continue;
            }
            $str1 = ltrim($matches_str[$i], '.');

            $root = 'https://www.chexinbaogao.com' . $str1;



            $body = file_get_contents($root);


            $new_root = explode('?', $str1);
            if(!file_exists($_SERVER['DOCUMENT_ROOT'] . "/commons/html/1/bg/js2"))
            {
                mkdir($_SERVER['DOCUMENT_ROOT'] . "/commons/html/1/bg/js2", 0777, true);
            }

            file_put_contents($_SERVER['DOCUMENT_ROOT'] . "/commons/html/1" . $new_root[0], $body);


        }

        file_put_contents($_SERVER['DOCUMENT_ROOT'] . "/commons/html/1/1.html", $aa);




        // $url = "https://www.chexinbaogao.com/tb2/ft-n1.png";

        echo $aa;

    }

    public function my_curl($url) {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_TIMEOUT, 10);
        $result = curl_exec($ch);
        curl_close($ch);
        return $result;
    }
}